package com.ruoyi.merchant.service.impl;

import java.util.List;

import com.ruoyi.common.core.constant.Constants;
import com.ruoyi.common.core.constant.SecurityConstants;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.exception.ServiceException;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.merchant.domain.ServiceType;
import com.ruoyi.merchant.domain.ShopServiceType;
import com.ruoyi.merchant.domain.ShopVerification;
import com.ruoyi.merchant.domain.vo.RegisterVo;
import com.ruoyi.merchant.domain.vo.Shops;
import com.ruoyi.merchant.mapper.ServiceTypeMapper;
import com.ruoyi.merchant.mapper.ShopServiceTypeMapper;
import com.ruoyi.merchant.mapper.ShopVerificationMapper;
import com.ruoyi.system.api.RemoteUserService;
import com.ruoyi.system.api.domain.SysUser;
import com.ruoyi.system.mapper.SysUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.SetOperations;
import org.springframework.stereotype.Service;
import com.ruoyi.merchant.mapper.ShopMapper;
import com.ruoyi.merchant.domain.Shop;
import com.ruoyi.merchant.service.IShopService;

import javax.annotation.Resource;

/**
 * 店铺Service业务层处理
 *
 * @author mage
 * @date 2024-09-12
 */
@Service
public class ShopServiceImpl implements IShopService {
    @Autowired
    private ShopMapper shopMapper;

    @Resource
    private ServiceTypeMapper serviceTypeMapper;

    @Autowired
    private ShopServiceTypeMapper shopServiceTypeMapper;

    @Resource
    private RedisTemplate<String, Object> redisTemplate;

    @Autowired
    private SysUserMapper sysUserMapper;
    @Autowired
    private ShopVerificationMapper shopVerificationMapper;

    @Autowired
    private RemoteUserService remoteUserService;


    /**
     * 查询店铺
     *
     * @param id 店铺主键
     * @return 店铺
     */
    @Override
    public Shop selectShopById(Long id) {
        return shopMapper.selectShopById(id);
    }

    /**
     * 查询店铺列表
     *
     * @param shop 店铺
     * @return 店铺
     */
    @Override
    public List<Shop> selectShopList(Shop shop) {
        List<ShopServiceType> shopServiceTypes = shopServiceTypeMapper.selectShopServiceTypeList(new ShopServiceType());
        List<Shop> shops = shopMapper.selectShopList(shop);
        for (Shop shop1 : shops) {
            Integer num = 0;
            for (ShopServiceType shopServiceType : shopServiceTypes) {
                if (shop1.getId() == shopServiceType.getMid()) {
                    num++;
                }
//                System.out.println(num);
            }
            Long[] serviceTypeIds = new Long[num];
            Integer index = 0;
            for (ShopServiceType shopServiceType : shopServiceTypes) {
                if (shop1.getId() == shopServiceType.getMid()) {
                    serviceTypeIds[index] = shopServiceType.getTypeId();
                    index++;
                }
                shop1.setServiceTypeIds(serviceTypeIds);
            }
//            System.out.println(serviceTypeIds.toString());
        }
        return shops;
    }

    /**
     * 新增店铺
     *
     * @param shop 店铺
     * @return 结果
     */
    @Override
    public int insertShop(Shop shop) {
        int num = shopMapper.insertShop(shop);
        Long[] serviceTypeIds = shop.getServiceTypeIds();
        for (Long serviceTypeId : serviceTypeIds) {
            ShopServiceType ServiceType = new ShopServiceType();
            ServiceType.setMid(shop.getId());
            ServiceType.setTypeId(serviceTypeId);
            shopServiceTypeMapper.insertShopServiceType(ServiceType);
        }
        // 注册用户信息
        ShopVerification shopVerification = shop.getShopVerification();
        SysUser sysUser = new SysUser();
        sysUser.setNickName(shopVerification.getMobile());
        sysUser.setUserName(shopVerification.getMobile());
        sysUser.setPhonenumber(shopVerification.getMobile());
        sysUser.setPassword(SecurityUtils.encryptPassword("123456"));
        R<?> registerResult = remoteUserService.registerUserInfo(sysUser, SecurityConstants.INNER);

        if (R.FAIL == registerResult.getCode()) {
            throw new ServiceException(registerResult.getMsg());
        }
        return num;
    }

    /**
     * 修改店铺
     *
     * @param shop 店铺
     * @return 结果
     */
    @Override
    public int updateShop(Shop shop) {
        if (shop.getServiceTypeIds() != null) {
            Long[] serviceTypeIds = shop.getServiceTypeIds();
            ShopServiceType serviceType=new ShopServiceType();
            serviceType.setMid(shop.getId());
            List<ShopServiceType> shopServiceTypes = shopServiceTypeMapper.selectShopServiceTypeList(serviceType);
            for (ShopServiceType shopServiceType : shopServiceTypes) {
                shopServiceTypeMapper.deleteShopServiceTypeById(shopServiceType.getId());
            }
            for (Long serviceTypeId : serviceTypeIds) {
                ShopServiceType ServiceType = new ShopServiceType();
                ServiceType.setMid(shop.getId());
                ServiceType.setTypeId(serviceTypeId);
                shopServiceTypeMapper.insertShopServiceType(ServiceType);
            }
        }
        if (shop.getAuditStatusId()==2 || shop.getAuditStatusId()==1) {
            Shop shop1 = shopMapper.selectShopById(shop.getId());
            SetOperations<String, Object> stringObjectSetOperations = redisTemplate.opsForSet();

            stringObjectSetOperations.add("shop_id:"+shop.getId(),shop1);


            Shops shops = new Shops();
            shops.setId(shop1.getId());
            shops.setLatitude(shop1.getLatitude());
            shops.setLongitude(shop1.getLongitude());
            ListOperations<String, Object> stringObjectListOperations = redisTemplate.opsForList();
            List<Object> shops1 = stringObjectListOperations.range("shops", 0, -1);
            Boolean num=false;
            for (Object shops2: shops1) {
                System.out.println("shops"+shops2);
                if (((Shops)shops2).getId() == shop1.getId()) {
                    num=true;
                }
            }
            if (num == false) {
                stringObjectListOperations.rightPush("shops",shops);
            }



        }
        return shopMapper.updateShop(shop);
    }

    /**
     * 批量删除店铺
     *
     * @param ids 需要删除的店铺主键
     * @return 结果
     */
    @Override
    public int deleteShopByIds(Long[] ids) {
        return shopMapper.deleteShopByIds(ids);
    }

    /**
     * 删除店铺信息
     *
     * @param id 店铺主键
     * @return 结果
     */
    @Override
    public int deleteShopById(Long id) {
        return shopMapper.deleteShopById(id);
    }

    @Override
    public int register(RegisterVo registerVo) {
        Shop shop=new Shop();
        shop.setName(registerVo.getName());
        shop.setDescription(registerVo.getDescription());
        shop.setOperatingArea(registerVo.getOperatingArea());
        shop.setServiceTypeIds(registerVo.getServiceTypeIds());
        shop.setAddress(registerVo.getAddress());
        shop.setLongitude(registerVo.getLongitude());
        shop.setLatitude(registerVo.getLatitude());
        shopMapper.insertShop(shop);
        ShopVerification shopVerification = new ShopVerification();
        shopVerification.setMid(shop.getId());
        shopVerification.setDirector(registerVo.getDirector());
        shopVerification.setFront(registerVo.getFront());
        shopVerification.setMobile(registerVo.getMobile());
        shopVerification.setOpposite(registerVo.getOpposite());
        shopVerification.setBusinessLicenseImg(registerVo.getBusinessLicenseImg());
        shopVerification.setBusinessLicenseId(registerVo.getBusinessLicenseId());
        shopVerification.setIdCard(registerVo.getIdCard());
        shopVerificationMapper.insertShopVerification(shopVerification);
        return 0;
    }
}
